package com.studyadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.studyadmin.config.QueryPageParam;
import com.studyadmin.entity.Chapter;
import com.studyadmin.entity.ChapterContent;
import com.studyadmin.mapper.ChapterContentMapper;
import com.studyadmin.mapper.ChapterMapper;
import com.studyadmin.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyadmin.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import java.beans.Transient;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 章节表 服务实现类
 * </p>
 *
 * @author lyl
 * @since 2023-03-19
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {
    @Autowired(required = false)
    ChapterMapper chapterMapper;
    @Autowired(required = false)
    ChapterContentMapper chapterContentMapper;

    @Override
    public JsonUtil chByCouIdSer(String CourseId) {
        JsonUtil jsonUtil = new JsonUtil();

        List<Chapter> chapters=chapterMapper.selectList(
               new QueryWrapper<Chapter>()
                       .eq("course_id",Integer.parseInt(CourseId))
        );
        if(chapters.size()<=0){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("未知错误");
        }else {
            jsonUtil.setCode(0);
            jsonUtil.setMsg("查询成功");
            jsonUtil.setData(chapters);
        }
        return jsonUtil;
    }

    @Override
    public JsonUtil chapterContentSer(String chaId) {

        JsonUtil jsonUtil = new JsonUtil();

        ChapterContent chapters=chapterContentMapper.selectOne(
                new QueryWrapper<ChapterContent>()
                        .eq("cha_id",Integer.parseInt(chaId))
        );
        if(chapters==null){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("未知错误");
        }else {
            jsonUtil.setCode(0);
            jsonUtil.setMsg("查询成功");
            jsonUtil.setData(chapters);
        }
        return jsonUtil;
    }

    /**
     * 添加章节
     * @param c
     * @return
     */
    @Transactional
    @Override
    public JsonUtil addChapterSer(Chapter c) {
        System.out.println(c);
        JsonUtil jsonUtil = new JsonUtil();
        Chapter chapter=chapterMapper.selectOne(
                new QueryWrapper<Chapter>()
                        .eq("cha_name",c.getChaName())
        );
        if(chapter!=null){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("此章节以存在");
        }else{
            c.setChaState(1);
            int res=chapterMapper.insert(c);

            ChapterContent chapterContent = new ChapterContent();
            chapterContent.setChaId(c.getChaId());
            chapterContent.setChaName(c.getChaName());
            chapterContent.setCreateTime(LocalDateTime.now());
            chapterContent.setUpdateTime(LocalDateTime.now());
            int resCon=chapterContentMapper.insert(chapterContent);




            if(res<0 || resCon<0){
                jsonUtil.setCode(1);
                jsonUtil.setMsg("未知错误");
            }else {
                jsonUtil.setCode(0);
                jsonUtil.setMsg("插入成功");
            }
        }

        return jsonUtil;
    }

    /**
     * 分页查询章节数
     * @param query
     * @return
     */
    @Override
    public JsonUtil pageChapterSer(QueryPageParam query) {
        System.out.println(query);
        HashMap param = query.getParam();
        String courseId = (String) param.get("courseId");      //课程ID
        Page<Chapter> chapterPage = new Page<>();
        chapterPage.setCurrent(query.getPageNum());    //设置每页多少条记录
        chapterPage.setSize(query.getPageSize());      //设置当前显示第几页
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", Integer.parseInt(courseId));
        queryWrapper.eq("cha_state",1);
//        IPage result = chapterMapper.pageChaMapper(chapterPage, queryWrapper); //最后进行分页查询
//
        IPage<Chapter> page=new Page<>(query.getPageNum(),query.getPageSize());
        IPage result =chapterMapper.selectPage(page,queryWrapper);


        return JsonUtil.suc(result.getRecords(), result.getTotal());


    }

    /**
     * 修改章节内容
     * @param query id content htmlcontent
     * @return
     */
    @Override
    public JsonUtil ChapterUpdateSer(ChapterContent query) {
        JsonUtil jsonUtil = new JsonUtil();
       //更新的字段
        // Query对象，用于设置条件，传User实体
        QueryWrapper<ChapterContent> wrapper = new QueryWrapper<>();
        wrapper.eq("cha_id",query.getChaId()); // 设置查询条件
        int res=chapterContentMapper.update(query, wrapper);

//        System.out.println(res);
        if(res<0){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("修改失败");
        }else {
            jsonUtil.setCode(0);
            jsonUtil.setMsg("修改成功");
        }
        return jsonUtil;
    }


    /**
     * 删除章节 修改状态
     * @param chapter
     * @return
     */
    @Override
    public JsonUtil delChapterSer(Chapter chapter) {


        JsonUtil jsonUtil = new JsonUtil();
        chapter.setChaState(0);
        int res=chapterMapper.updateById(chapter);
        if(res<0){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("删除失败");

        }else {
            jsonUtil.setCode(0);
            jsonUtil.setMsg("删除成功");
        }

        return jsonUtil;
    }


}
